import ProcessActionPromiseFactory from "../../utils/promise.js";
Page({
  data: {
    genderList:['女','男','隐藏'],
    nameInput:'',
    addressInput:'',
    isAltered:false
  },
  bindTapProfilePic(){
    var that = this;
    var app = getApp();
    var hostAddress = app.globalData.hostAddress;
    var fPath = '';
    ProcessActionPromiseFactory((resolve,reject)=>{
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        success(res) {
          fPath = res.tempFiles[0].tempFilePath;
          resolve(fPath);
        }
      });
    })
    .then(res=>{
      return ProcessActionPromiseFactory((resolve,reject)=>{
        var fPath = res;
        wx.uploadFile({
          url: hostAddress+'/gallaryMarket/interface/Filter.php', 
          filePath: fPath,
          name: 'file',
          formData: {
            'role': 'Customer',
            'action': 'insertHimage',
            'cid':app.globalData.clientInfo.cid
          },
          success (res){
            let resArr = res.data.split('|');
            if(resArr[0]==1){
              wx.showToast({
                title: '保存成功！',
                icon: 'success',
                duration: 1000 //持续的时间
              });
              resolve(resArr[1]);
            }else{
              wx.showToast({
                title: '保存失败！',
                icon: 'error',
                duration: 1000 //持续的时间
              });
              reject("profileImageInsertError!");
            }
          }
        });
      })
    })
    .then(res=>{
      return ProcessActionPromiseFactory((resolve,reject)=>{
        setTimeout(()=>{
          let newPath = res;
          let newClientInfo = that.data.clientInfo;
          //更新当前
          newClientInfo.profilePicUrl = newPath;
          that.setData({clientInfo:{}});
          that.setData({clientInfo:newClientInfo});
          //更新全局
          app.globalData.clientInfo.profilePicUrl = newPath;
          resolve();
        },100);
      })
    })
    .catch(err=>{
      console.log(err);
    });
  },  
  bindInputListener(e){
    let value = e.detail.value;
    e.target.dataset.id=="name"?this.setData({nameInput:value}):this.setData({addressInput:value});
    if(this.data.isAltered) return;
    this.setData({isAltered:true});//通知状态改变
  },
  bindBlurListener(e){
    let value = e.detail.value;
    let newClientInfo = this.data.clientInfo;
    newClientInfo[e.target.dataset.id=="name"?"username":"address"] = value;
    this.setData({clientInfo:null});
    this.setData({clientInfo:newClientInfo});
  },
  bindChangePicker(e){
    //性别更改
    var newInfo = this.data.clientInfo;
    if(e.target.dataset.id=="gender"){
      newInfo.gender = +e.detail.value;
    }else{
      newInfo.birthday = e.detail.value;
    }
    this.setData({clientInfo:null});
    this.setData({clientInfo:newInfo});
    if(this.data.isAltered) return;
    this.setData({isAltered:true});//通知状态改变
  },
  bindTapClear(){
    if(this.data.isAltered==false) return;
    this.setClientInfo();
    this.setData({isAltered:false});
  },
  bindTapSave(){
    let clientInfo = this.data.clientInfo;
    let submitInfo = {};
    let submitItem = ['address','username','birthday','gender','cid'];//要保留的键
    for (let item of submitItem){
      submitInfo[item] = clientInfo[item];
    }
    (getApp()).pinia('alterClientInfo',{
      submitInfo:JSON.stringify(submitInfo)
    },(res)=>{
      wx.showToast({
        title:'保存成功',
        icon:'success',
        duration:1000
      });
      (getApp()).globalData.clientInfo = res.data;
      this.setClientInfo();
    })
  },
  bindTapLogin(){
    var app = getApp();
    var that = this;
    wx.login({//获取code
      success:(res)=>{
        var code = res.code;
        app.pinia("login",{//获取cid
          'code':code
        },(res)=>{
          console.log("cid:",res.data)
          let cid = res.data.cid;
          app.pinia("checkBasicInfo",{//获取用户信息
            'cid':cid
          },(res)=>{
            wx,wx.setStorageSync('gallerySuperMarketToken', res.data.openid);
            app.globalData.clientInfo = [res.data];
            that.setData({
              clientInfo:res.data
            });
            wx.showToast({
              title: '登录成功',
              icon:'success',
              duration:1000
            });
          })
        });
      }
    });
  },
  setClientInfo(){
    //拷贝
    let globalClientInfo = (getApp()).globalData.clientInfo;
    let copyClientInfo = {};
    for (let key in globalClientInfo){
      copyClientInfo[key] = globalClientInfo[key];
    }
    this.setData({clientInfo:copyClientInfo});//设置个人资料
  },
  onLoad(options) {
    this.setData({themeIndex:(getApp()).globalData.themeIndex});//设置主题
    this.setClientInfo();//设置用户信息
  }
})